package com.ljh.redis.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: Themberfue
 * @date: 2025/4/5 15:42
 * @description:
 */
@RestController
public class HashController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/hash")
    public Map<Object, Object> hash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("k1", "f1", "v1");
        Map<String, String> fv = Map.of("f2", "v2", "f3", "v3", "f4", "v4");
        redisTemplate.opsForHash().putAll("k1", fv);

        Long size = redisTemplate.opsForHash().size("k1");
        System.out.println("size:" + size);

        Boolean hasKey = redisTemplate.opsForHash().hasKey("k1", "f1");
        System.out.println("hasKey:" + hasKey);

        Long delete = redisTemplate.opsForHash().delete("k1", "f4");
        System.out.println("delete:" + delete);

        return redisTemplate.opsForHash().entries("k1");
    }
}
